﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Threading;
using System.Diagnostics;

namespace MineLaunch
{
	/// <summary>
	/// App.xaml の相互作用ロジック
	/// </summary>
	public partial class App : Application
	{
		private static Window view;
		private static Mutex mutex;
		private const string MutexName = "MineLaunch_5CA7BCFF-088F-49C1-A6AA-8F03BD3A94BB";

		public static Window GetView()
		{
			return view;
		}

		private void Application_Startup(object sender, StartupEventArgs e)
		{
			InitializeThemes();
			mutex = new Mutex(false, MutexName);
			if (!mutex.WaitOne(0, false))
			{
				Console.WriteLine("すでに起動してるよ！");
				mutex.Close();
				mutex = null;
				this.Shutdown();
				return;
			}
			view = new MainWindow();
			view.Show();
		}

		private void Application_Exit(object sender, ExitEventArgs e)
		{
			if (mutex != null)
			{
				mutex.ReleaseMutex();
				mutex.Close();
			}
		}

		private void InitializeThemes()
		{
			Themes.Add("Default", "ThemeMain");
			Themes.Add("Aero", "ThemeAero");

			ChangeTheme("ThemeAero");
		}

		public Dictionary<string, string> Themes = new Dictionary<string, string>();

		private ResourceDictionary dictionary;
		public void ChangeTheme(string themeName)
		{
			if (dictionary == null)
			{
				dictionary = new ResourceDictionary();
				Application.Current.Resources.MergedDictionaries.Add(dictionary);
			}
			string themeUri = String.Format("pack://application:,,,/ResoueceDictionaries/{0}.xaml", themeName);
			dictionary.Source = new Uri(themeUri);
		}
	}
}
